﻿using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace OnWork.DataCenter.Utils
{
    /// <summary>
    /// Json帮助类
    /// </summary>
    public class JsonHelper
    {
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="json"></param>
        /// <returns></returns>
        public static TValue Deserialize<TValue>(string json) where TValue : class
        {
            return System.Text.Json.JsonSerializer.Deserialize<TValue>(json);
        }

        /// <summary>
        /// 序列化设置
        /// </summary>
        private static System.Text.Json.JsonSerializerOptions SerializeOpt = new System.Text.Json.JsonSerializerOptions()
        {
            WriteIndented = true,
        };

        /// <summary>
        /// 序列化 不格式化
        /// </summary>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="json"></param>
        /// <returns></returns>
        public static string Serialize<TValue>(TValue obj) where TValue : class
        {
            return System.Text.Json.JsonSerializer.Serialize<TValue>(obj);
        }

        /// <summary>
        /// 序列化格式化形式
        /// </summary>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="json"></param>
        /// <returns></returns>
        public static string SerializeFormat<TValue>(TValue obj) where TValue : class
        {
            return System.Text.Json.JsonSerializer.Serialize<TValue>(obj, SerializeOpt);
        }

        /// <summary>
        /// Json格式化
        /// </summary>
        /// <param name="json">json字符串</param>
        /// <param name="type">格式化方法不同 0/1</param>
        /// <returns>格式化后的字符串</returns>
        public static string JsonFormatOnWork(string json, int type = 1)
        {
            //两种Json方法
            if (type == 0)
            {
                JsonSerializer serializer = new JsonSerializer();
                TextReader tr = new StringReader(json);
                JsonTextReader jtr = new JsonTextReader(tr);
                object obj = serializer.Deserialize(jtr);
                if (obj != null)
                {
                    StringWriter textWriter = new StringWriter();
                    JsonTextWriter jsonWriter = new JsonTextWriter(textWriter)
                    {
                        Formatting = Formatting.Indented,
                        Indentation = 4,//缩进字符数
                        IndentChar = ' '//缩进字符
                    };
                    serializer.Serialize(jsonWriter, obj);
                    return textWriter.ToString();
                }
                return json;
            }
            else
            {
                //JObject.ToString()方法会内部调用格式化，所以直接使用即可

                //判读是数组还是对象
                if (json.StartsWith("["))
                {
                    JArray jobj = JArray.Parse(json);
                    return jobj.ToString();
                }
                else if (json.StartsWith("{"))
                {
                    JObject jobj = JObject.Parse(json);
                    return jobj.ToString();
                }
            }
            return json;
        }
    }
}